﻿using UnityEngine;
using System.Collections;

public class TeethParticleScript : MonoBehaviour 
{
    public static TeethParticleScript instance;
    bool isOn = false;

    void Awake()
    {
        instance = this; 
        transform.FindChild("ParticleSystem").GetComponent<ParticleSystem>().Stop();
        transform.FindChild("ParticleSystem").GetComponent<ParticleSystem>().Clear();
    }

	void Start () 
    {
      
	}	
	
	void Update () 
    {
	
	}

    public void StopAndClear()
    {
        transform.FindChild("ParticleSystem").GetComponent<ParticleSystem>().Stop();
        transform.FindChild("ParticleSystem").GetComponent<ParticleSystem>().Clear();
    }

    public void StartAt(Vector3 position)
    {
        if (isOn)
        {
            StopCoroutine("WaitAndStop");
            transform.FindChild("ParticleSystem").GetComponent<ParticleSystem>().Clear();
            transform.FindChild("ParticleSystem").GetComponent<ParticleSystem>().Stop();
        }

        isOn = true;
        transform.position = position;
        transform.FindChild("ParticleSystem").GetComponent<ParticleSystem>().Play();
        StartCoroutine("WaitAndStop");    
    }

    private IEnumerator WaitAndStop()
    {
       yield return new WaitForSeconds(0.5f);
       transform.FindChild("ParticleSystem").GetComponent<ParticleSystem>().Stop();
       isOn = false;
    }

}
